package com.douwong.student.task;

import android.os.AsyncTask;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.contant.NetConfig;
import com.douwong.student.interfaces.WorkResultNotify;
import com.douwong.student.model.ClassFile;
import com.douwong.student.model.PrepareBean;
import com.douwong.student.model.RequestAction;
import com.douwong.student.model.StatusCode;
import com.douwong.student.utils.NetUtils;
import com.douwong.student.utils.UIUtils;

import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

public class ClassFileTask extends AsyncTask<String, Integer, Integer> {

	private WorkResultNotify<ClassFile> clientDone;
	private BaseApplication app;
	
	private RequestAction action;
	public static int NO_DATA = 0;// 没有新的数据
	public static int REQUESTOK = 1;// 加载更多
	public static int NO_NETWOK = -1;// 没有网络
	public static int ERROR=2;
	private ArrayList<ClassFile>list;
	private String key = null;
	private boolean isSearch=false;
	public ClassFileTask(boolean isSearch, RequestAction action, WorkResultNotify<ClassFile>  clientDone, BaseApplication app){
		this.clientDone=clientDone;
		this.app=app;
		this.action=action;
		this.isSearch=isSearch;
	}
	@SuppressWarnings("unchecked")
	@Override
	protected Integer doInBackground(String... params) {
		if(params[0].equals("1")){//课堂笔记
			key=BaseApplication.sUserBean.getUserbean().getEuNumber()+"_notes";
		}else if(params[0].equals("2")){//小黑板
			key=BaseApplication.sUserBean.getUserbean().getEuNumber()+"_black";
		}else if(params[0].equals("3")){//视频
			key=BaseApplication.sUserBean.getUserbean().getEuNumber()+"_video";
		}
		if(RequestAction.INIT==action){
			list=(ArrayList<ClassFile>) app.readObject(key);
			if(list!=null&&list.size()>0){
				return REQUESTOK;
			}else if(NetUtils.NetwrokIsUseful(UIUtils.getContext())){
				return accessNetworkRequestData(params);
			}else {
				return NO_NETWOK;
			}
		}else {
			if(NetUtils.NetwrokIsUseful(UIUtils.getContext())){
				return accessNetworkRequestData(params);
			}else {
				return NO_NETWOK;
			}
		}
	}

	@Override
	protected void onPostExecute(Integer result) {
		super.onPostExecute(result);
		if(clientDone!=null){
			clientDone.notify(result, list);
		}
	}
	private int accessNetworkRequestData(String... params) {
		String response = null;
		int resultCode = REQUESTOK;
		List<BasicNameValuePair> arrayParams = new ArrayList<BasicNameValuePair>();
		arrayParams.add(new BasicNameValuePair("userId", BaseApplication.sUserBean
				.getUserbean().getId()+""));
		arrayParams.add(new BasicNameValuePair("classId", BaseApplication.sUserBean
				.getPersonalbean().getClassId()+""));
		if(params[0]!=null){
			arrayParams.add(new BasicNameValuePair("moduleSource", params[0]));
		}
		if(params[1]!=null){
			arrayParams.add(new BasicNameValuePair("subjectId", params[1]));
		}
		if(params[2]!=null){
			arrayParams.add(new BasicNameValuePair("startTime", params[2]));
		}
		if(params[3]!=null){
			arrayParams.add(new BasicNameValuePair("endTime", params[3]));
		}
		arrayParams.add(new BasicNameValuePair("rowsPerPage", "30"));
		arrayParams.add(new BasicNameValuePair("page", params[4]));
		try {
			response = NetUtils
					.httpGet(NetConfig.CLASS_FILE, arrayParams);
			StatusCode<PrepareBean> sc = (StatusCode<PrepareBean>) JSON
					.parseObject(response,
							new TypeReference<StatusCode<PrepareBean>>() {
							});
			Log.e("response", response+"");
			if (sc!=null&&sc.isFlag()) {
				if ((sc.getData().getRows() == null
						|| sc.getData().getRows().equals(""))&& RequestAction.REFRESH == action)
						 {
					app.saveObject(list, key);
					return NO_DATA;
				}

				if ((sc.getData().getRows().equals("[]")||sc.getData().getRows() == null || sc.getData().getRows()
						.equals(""))&&action==RequestAction.LOADMROE) {
					app.saveObject(list, key);
					return NO_DATA;
				}
				list = (ArrayList<ClassFile>) JSON.parseArray(sc.getData()
						.getRows(), ClassFile.class);
				if(action==RequestAction.REFRESH){
					if(!isSearch){
						app.saveObject(list, key);
					}
				}else if(action==RequestAction.INIT){
					app.saveObject(list, key);
				}
			}else {
				return NO_DATA;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return resultCode;
	}
}
